﻿// Bravura : A music composition and audio synthesis framework.
//
// Copyright © 2011 Paul Vanukoff (pvanukoff@gmail.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.

namespace Bravura.Contracts.Directives
{
	/// <summary>
	/// Repeats and optionally modifies a series of notes over time.
	/// </summary>
	public class AutoNote : AutoBase<Note>
	{
		#region Properties

		public double SoundFrom
		{
			get;
			set;
		}

		public double SoundTo
		{
			get;
			set;
		}

		public double PitchFrom
		{
			get;
			set;
		}

		public double PitchTo
		{
			get;
			set;
		}

		public double VelocityFrom
		{
			get;
			set;
		}

		public double VelocityTo
		{
			get;
			set;
		}

		#endregion

		public AutoNote() 
		{
			this.SoundFrom = 0.0;
			this.SoundTo = 0.0;
			this.PitchFrom = 60.0;
			this.PitchTo = 60.0;
			this.VelocityFrom = 1.0;
			this.VelocityTo = 1.0;
		}

		protected override Note Generate(Note dir, int relTick)
		{
			double k = 1.0 * (relTick + dir.NoteOnTime) / Delta;

			Note gen = new Note();
			gen.NoteOnTime = TickStart + dir.NoteOnTime + relTick;
			gen.NoteOffTime = TickStart + dir.NoteOffTime + relTick;
			gen.Voice = dir.Voice;
			gen.Sound = dir.Sound ?? SoundFrom + k * (SoundTo - SoundFrom);
			gen.Pitch = dir.Pitch ?? PitchFrom + k * (PitchTo - PitchFrom);
			gen.Velocity = dir.Velocity ?? VelocityFrom + k * (VelocityTo - VelocityFrom);
			gen.NoteOnControls = dir.NoteOnControls;
			gen.NoteOnSettings = dir.NoteOnSettings;

			return gen;
		}
	}
}
